/**
*add by cjj 销售品保存
*/
const prodSave = {
    buildObject: (res, agentList,busiManagerId,custManagerId) => {
        // let prodSaveInfo = {}; //存放接口报文
        // let contractRoot = {};
        // let svcCont = {};
        let requestObject = {};
        requestObject.custOrderId = res.custOrderId;
        requestObject.custSoNumber = res.custSoNumber;
        requestObject.operType = res.operType;
        requestObject.custOrderType = res.custOrderType;
        requestObject.orderSourceId = res.orderSourceId;
        requestObject.createStaff = res.createStaff;
        requestObject.createOrgId = res.createOrgId;
        requestObject.accepteRegionId = res.accepteRegionId;
        requestObject.accepteLanId = res.accepteLanId;
        requestObject.custId = res.custId;
        requestObject.custName = res.custName;
        requestObject.sysSource = res.sysSource;
        requestObject.channelId = res.channelId;
        requestObject.orderSource = res.orderSource;
        requestObject.bussDesc = res.bussDesc;
        requestObject.remark = res.remark;
        requestObject.contactId = res.contactId;
        requestObject.contactCd = res.contactCd;
        requestObject.contactName = res.contactName;
        requestObject.contactNumberType = res.contactNumberType;
        requestObject.projectId = res.projectId;
        requestObject.projectName = res.projectName;
        requestObject.projectManage = res.projectManage;
        requestObject.saleId = res.saleId;
        requestObject.extCustOrderId = res.extCustOrderId;
        var buildHandle = prodSave.buildHandle(agentList);//经办人信息
        if (Object.keys(buildHandle).length > 0) {
            requestObject.order_Handler = buildHandle;
        }
        requestObject.mainOfferInsts = prodSave.buildMainOffer(res.result,busiManagerId,custManagerId);//主销售品
        // svcCont.requestObject = requestObject;
        // contractRoot.svcCont = svcCont;
        // prodSaveInfo.contractRoot = contractRoot;
        return requestObject;
    },

    buildHandle: (res) => {//经办人信息
        let order_Handler = {}; //经办人信息
        res.forEach((item) => {
            let tableName = item.tableName; 
            let colId = item.colId;
            let value = item.value;
            if (tableName == "ORDER_HANDLER") {
                order_Handler[prodSave.toHump(colId)] = value;
            }
        });
        return order_Handler;
    },

    buildMainOffer: (res,busiManagerId,custManagerId) => {//主销售品
        var mainOfferInsts = [];
        res.forEach((item) => {
            var obj = prodSave.buildMainOfferObj(item,busiManagerId,custManagerId);
            mainOfferInsts.push(obj)
        })
        return mainOfferInsts;
    },

    buildMainOfferObj: (res,busiManagerId,custManagerId) => {//销售品分支
        var obj = {};
        obj.offerId = '10010021';//销售品标识 前一页面传回
        obj.serviceOfferId = "4010100000";//操作id", 新装4010100000
        res.forEach((item) => {//查询ORDER_ITEM
            if (item.tableName == 'ORDER_ITEM') {
                if (item.colId == 'ORDER_DATE') {//要求完成时间需求单用reqOrderDate，订单用orderDate
                    item.colId = 'REQ_ORDER_DATE'
                }
                obj[prodSave.toHump(item.colId)] = item.value;
            }
        })
        obj.orderItemAttrs = prodSave.buildItemAttr(res);//查询ORDER_ITEM_ATTR
        obj.orderContactInfos = prodSave.buildContactInfos(res);//查询甲乙端信息ORDER_CONTACT_INFO  2.0表名PARTY_CONTACT_INDO
        obj.orderItemFiles = prodSave.buildItemFiles(res);//附件ORDER_ITEM_FILE
        obj.acctItemOrgs = prodSave.buildItemOrgs(res);//一次费ACCT_ITEM_ORG
        obj.ordProdInsts = prodSave.buildProdInst(res,busiManagerId,custManagerId);//产品实例PROD_INST

        return obj;
    },

    buildItemAttr: (res) => {
        var orderItemAttrs = [];
        res.forEach((res) => {
            var obj = {};
            if (res.tableName == 'ORDER_ITEM_ATTR') {
                obj.attrId = res.colId;
                obj.attrValue = res.value;
                obj.attrCd = res.fieldId;
                orderItemAttrs.push(obj);
            }
        })
        return orderItemAttrs;
    },

    buildContactInfos: (res) => {//甲乙端信息
        var orderContactInfos = [];
        res.forEach((res) => {
            var obj = {};
            if (res.tableName == 'ORDER_CONTACT_INFO') {
                if (res.attrRoleCd == '100') { obj.contactType = '6'; } if (res.attrRoleCd == '200') { obj.contactType = '7'; }
                obj[prodSave.toHump(res.colId)] = res.value;
                orderContactInfos.push(obj);
            }
        })
        let orderContactInfo = prodSave.mergeObject(orderContactInfos, 'contactType')
        return orderContactInfo;
    },

    buildItemFiles: (res) => {//附件
        let orderItemFiles = [];
        res.forEach((res) => {
            var obj = {};
            if (res.tableName == 'ORDER_ITEM_FILE') {
                obj[prodSave.toHump(res.colId)] = res.value;
                orderItemFiles.push(obj);
            }
        })
        let orderItemFile = prodSave.mergeObject(orderItemFiles, '')
        return orderItemFile;
    },

    buildItemOrgs: (res) => {//资费信息
        let acctItemOrgs = [];
        res.forEach((item) => {
            var obj = {};
            if (item.tableName == 'ACCT_ITEM_ORG') {
                obj.chargeOrgCd = item.attrRoleCd;
                obj[prodSave.toHump(item.colId)] = item.value;
                if (item.attrRoleCd == '100') {
                    let acctItems = prodSave.buildAcctItem(res, item.attrRoleCd);//一次费帐目类型标识分组ACCT_ITEM
                    obj.acctItems = acctItems;
                }
                if (item.attrRoleCd == '200') {
                    let acctItems = prodSave.buildAcctItem(res, item.attrRoleCd);//一次费帐目类型标识分组ACCT_ITEM
                    obj.acctItems = acctItems;
                }
                if (item.attrRoleCd == '0' || !item.attrRoleCd) {
                    let acctItems = prodSave.buildAcctItem(res, item.attrRoleCd);//一次费帐目类型标识分组ACCT_ITEM
                    obj.acctItems = acctItems;
                }
                acctItemOrgs.push(obj);
            }
        })
        
        let acctItemOrg = prodSave.mergeObject(acctItemOrgs, 'chargeOrgCd');
        return acctItemOrg;
    },

    buildAcctItem: (res, attrRoleCd) => {//一次费帐目类型标识分组
        let acctItems = [];
        let newArr = res.filter((item) => {
            return item.tableName == 'ACCT_ITEM' && item.colId !== 'AMOUNT'
        })

        let newMount = res.filter((item) => {
            return item.tableName == 'ACCT_ITEM' && item.colId == 'AMOUNT'
        })

        newMount.forEach((item) => {
            let mapArr = '';
            let obj = {};
            if (item.attrRoleCd == attrRoleCd) {   
                mapArr = item.relIds.split(";")
                obj.acctItemTypeId = item.acctItemId;
                obj.amount = item.value;
                obj.acctTypeCd = item.attrRoleCd == item.attrRoleCd ? item.attrRoleCd : '0';
                acctItems = prodSave.getArr(mapArr, newArr, obj, acctItems);
            }
        })

        var acctItem = prodSave.mergeObject(acctItems, 'acctItemTypeId');
        return acctItem;
    },

    getArr: (mapArr, newArr, obj, acctItems) => {
        for (var i = 0; i < mapArr.length; i++) {
            let obj1 = {};
            for (var j = 0; j < newArr.length; j++) {
                let id = newArr[j].id.split('-')[1];
                if (mapArr[i].includes(id)) {
                    obj1[prodSave.toHump(newArr[j].colId)] = newArr[j].value;
                }
            }
            var obj2 = { ...obj, ...obj1 }
            acctItems.push(obj2);
        }
        return acctItems;
    },

    buildProdInst: (res,busiManagerId,custManagerId) => {//产品实例
        let ordProdInsts = [];
        res.forEach((item) => {
            let obj = {};
            if (item.tableName == 'PROD_INST') {
                if (item.attrRoleCd == '100') {
                    obj.prodId = '80007004';//产品ID需要前一个页面选择销售品的时候带过来
                    obj.prodOfferId = '10010021';//确认是否换成offerId//销售品ID需要前一个页面选择销售品的时候带过来
                    obj.roleCd = '100';
                    obj.accNum = '123456';//临时添加跟中台调试用  2021/4/23
                    obj.custManagerId = custManagerId;//前一个页面选择带过来
                    obj.busiManagerId = busiManagerId;//前一个页面选择带过来
                    obj.respCustId = '1223';//存的省客户ID前一个页面带过来
                    obj[prodSave.toHump(item.colId)] = item.value;
                }
                if (item.attrRoleCd == '200') {
                    obj.prodId = '80007004';//产品ID需要前一个页面选择销售品的时候带过来
                    obj.prodOfferId = '10010021';//确认是否换成offerId//销售品ID需要前一个页面选择销售品的时候带过来
                    obj.roleCd = '200';
                    obj.accNum = '123456';//临时添加跟中台调试用  2021/4/23
                    obj.custManagerId = custManagerId;//前一个页面选择带过来
                    obj.busiManagerId = busiManagerId;//前一个页面选择带过来
                    obj.respCustId = '1343';//存的省客户ID前一个页面带过来
                    obj[prodSave.toHump(item.colId)] = item.value;
                }
                if (item.attrRoleCd == '0' || !item.attrRoleCd) {
                    obj.prodId = '80008007';//产品ID需要前一个页面选择销售品的时候带过来
                    obj.prodOfferId = '10010021';//确认是否换成offerId//销售品ID需要前一个页面选择销售品的时候带过来
                    obj.roleCd = '0';
                    obj.accNum = '123456';//临时添加跟中台调试用  2021/4/23
                    obj.custManagerId = custManagerId;//前一个页面选择带过来
                    obj.busiManagerId = busiManagerId;//前一个页面选择带过来
                    obj.respCustId = '456';//存的省客户ID前一个页面带过来
                    obj[prodSave.toHump(item.colId)] = item.value;
                }
                obj.ordProdInstAttrs = prodSave.buildProdInstAttr(res, item.attrRoleCd, 'PROD_INST_ATTR');//PROD_INST_ATTR
                obj.ordProdInstAcctRels = prodSave.buildInstAcctRel(res, item.attrRoleCd);//PROD_INST_ACCT取页面表PROD_OFFER_FEE_INFO
                obj.ordOfferInstFeeInfos = prodSave.buildInstAcctRel(res, item.attrRoleCd);//PROD_OFFER_FEE_INFO取页面表PROD_OFFER_FEE_INFO
                ordProdInsts.push(obj);
            }
        });
        var ordProdInst = prodSave.mergeObject(ordProdInsts, 'roleCd');
        return ordProdInst;
    },

    buildProdInstAttr: (res, RoleCd, tabName) => {
        let ordProdInstAttrs = [];
        res.forEach((item) => {
            if (item.attrRoleCd == RoleCd) {
                let obj = {};
                if (item.tableName == tabName) {
                    obj.attrId = item.colId;
                    obj.attrValue = item.value;
                    obj.attrCd = item.fieldId;
                    ordProdInstAttrs.push(obj)
                }
            }
        })
        return ordProdInstAttrs;
    },

    buildInstAcctRel: (res, RoleCd) => {
        let ordOfferInstFeeInfos = [];
        let newArr = res.filter((item) => {
            return item.tableName == 'PROD_OFFER_FEE_INFO' && item.colId !== 'AMOUNT'
        })

        let newMount = res.filter((item) => {
            return item.tableName == 'PROD_OFFER_FEE_INFO' && item.colId == 'AMOUNT'
        })

        newMount.forEach((item) => {
            let mapArr = '';
            let obj = {};
            if (item.attrRoleCd == RoleCd) {
                mapArr = item.relIds.split(";")
                obj.acctItemTypeId = item.acctItemId;
                obj.acctItemGroupId = item.acctItemId;
                res.forEach((item) => {
                    if (item.tableName == 'PROD_INST' && item.colId == 'ACCOUNT_ID') {
                        obj.acctId = item.value
                    }
                })
                obj.amount = item.value;
                obj.roleCd = item.attrRoleCd;
                obj.odrOfferInstFeeAttrs = prodSave.buildProdInstAttr(res, RoleCd, 'PROD_OFFER_FEE_ATTR');//PROD_OFFER_FEE_ATTR
                ordOfferInstFeeInfos = prodSave.getArr(mapArr, newArr, obj, ordOfferInstFeeInfos);
            }
        })

        var ordOfferInstFeeInfo = prodSave.mergeObject(ordOfferInstFeeInfos, 'acctItemTypeId');
        return ordOfferInstFeeInfo;
    },

    mergeObject: (arr, key) => {//将数组中特定属性相同的对象合并
        var intype = {};
        var newJson = [];
        arr.forEach((item) => {
            if (intype[item[key]]) {
                Object.assign(newJson[intype[item[key]] - 1], item);
            } else {
                newJson.push(item);
                intype[item[key]] = newJson.length;
            }
        });
        return newJson;
    },

    toHump: (name) => {
        //驼峰转换
        let key = name.toLowerCase();
        return key.replace(/_(\w)/g, function (all, letter) {
            return letter.toUpperCase();
        });
    },



};

export default prodSave;
